package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.cardmanagement.util.TransferInOutStockApproval;
import com.turing.post.cardmanagement.util.TransferInOutStockApprovalBusiness;
import com.turing.post.cardmanagement.util.TransferInOutStockApprovalDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueBranch;
import com.turing.post.organization.util.IssueBranchDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.TransferInOutStockApprovalBusinessImpl")
@Remote
@Local
public class TransferInOutStockApprovalBusinessImpl implements
		TransferInOutStockApprovalBusiness {
	@EJB(mappedName = "cardmanagement.impl.TransferInOutStockApprovalDaoImpl")
	private TransferInOutStockApprovalDao transferInOutStockApprovalDao;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;

	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;

	@EJB(mappedName = "organization.impl.IssueBranchDaoImpl")
	private IssueBranchDao issueBranchDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao PCPSCommonDaoImpl;

	@Override
	public SysBranch getToTransferBranchNo(PCPSWebInput webInput) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		SysBranch sysBranch = sysBranchDaoImpl.getSysBranchByBranchNo(webInput
				.getOperBranchNo());
		String operBranchNo = webInput.getOperBranchNo();
		// 销售商机构
		if (SysBranch.SYSBRANCH_SALEBRANCH.equals(sysBranch.getBranchType())) {
			// 获取上级销售机构
			SaleBranch superSb = saleBranchDaoImpl
					.getSaleBranchBySaleBranchNo(operBranchNo);
			if (!PCPSUtil.isEmpty(superSb.getSuperSaleBranchNo())) {
				return new SysBranch(superSb.getSuperSaleBranchNo(), superSb
						.getSaleBranchName());
			} else {
				// 上级销售机构为空，获取发卡机构
				IssueBranch superIb = issueBranchDaoImpl
						.getIssueBranchByIssueBranchNo(superSb
								.getIssueBranchNo());
				return new SysBranch(superIb.getIssueBranchNo(), superIb
						.getIssueBranchName());
			}
		}
		// 发卡商机构
		if (SysBranch.SYSBRANCH_ISSUEBRANCH.equals(sysBranch.getBranchType())) {
			// 获取发卡商机构上级直属机构
			IssueBranch superIb = issueBranchDaoImpl
					.getIssueBranchByIssueBranchNo(operBranchNo);
			// 上级机构号不为空时
			if (!PCPSUtil.isEmpty(superIb.getSuperIssueBranchNo())) {
				return new SysBranch(superIb.getSuperIssueBranchNo(),
						issueBranchDaoImpl.getIssueBranchByIssueBranchNo(
								superIb.getSuperIssueBranchNo())
								.getIssueBranchName());
			}
		}
		return null;
	}

	@Override
	public BigInteger insertTransferInOutStockApproval(PCPSWebInput webInput,
			TransferInOutStockApproval vo) throws PCPSApplicationException {
		if (vo == null || webInput == null
				|| PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "", "参数为空!");

		if (PCPSUtil.isEmpty(vo.getBranchNo())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "", "申请机构为空!");
		}

		vo.setTransferApprovalId(PCPSCommonDaoImpl.getUniqueIntegerKey());
		vo.setApprovalBranchNo(webInput.getOperBranchNo());
		vo
				.setStatus(TransferInOutStockApproval.TRANSFERINOUTSTOCKAPPROVAL_STATUS_APPLY);
		vo.setAuthCount(BigInteger.ZERO);
		vo.setApprovalOperId(webInput.getOperId());
		vo.setTranTime(PCPSCommonDaoImpl.getDatabaseSysTime());

		transferInOutStockApprovalDao.insertTransferInOutStockApproval(vo);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "调拨单申请添加", "ApprovalBranchNo:"
						+ vo.getApprovalBranchNo());
		return vo.getApprovalOperId();
	}

	@Override
	public void deleteTransferInOutStockApproval(PCPSWebInput webInput,
			BigInteger TransferApprovalId) throws PCPSApplicationException {
		if (TransferApprovalId == null || webInput == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "", "参数为空!");
		TransferInOutStockApproval tiosa = transferInOutStockApprovalDao
				.getTransferInOutStockApprovalByTransferApprovalId(TransferApprovalId);
		if (tiosa == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "",
					"此对象不存在,无法删除!");
		if (tiosa.getApprovalOperId().compareTo(webInput.getOperId()) != 0) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_UNKNOWN, "",
					"必须由申请人删除此申请单!");
		}
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "调拨单申请删除", "ApprovalBranchNo:"
						+ tiosa.getApprovalBranchNo());
		transferInOutStockApprovalDao.deleteTransferInOutStockApproval(tiosa);
	}

	@Override
	public void AuthTransferInOutStockApprovalAdopt(PCPSWebInput webInput,
			TransferInOutStockApproval vo) throws PCPSApplicationException {
		if (vo == null || vo.getTransferApprovalId() == null
				|| vo.getAuthCount() == null || webInput == null
				|| webInput.getOperBranchNo() == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "", "参数为空!");
		TransferInOutStockApproval tiosa = transferInOutStockApprovalDao
				.getTransferInOutStockApprovalByTransferApprovalId(vo
						.getTransferApprovalId());
		if (tiosa == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "",
					"此对象不存在,无法审核通过!");
		if (!tiosa.getBranchNo().equals(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "",
					"不能操作其它机构调拨申请单!");
		}
		if (Integer.parseInt(vo.getAuthCount().toString()) <= 0) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_UNKNOWN, "",
					"批准卡张数不能小于等于0!");
		}
		if (tiosa.getApprovalOperId() == webInput.getOperId()) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_UNKNOWN, "",
					"申请人与审核人不能为同一人!");
		}
		tiosa.setAuthCount(vo.getAuthCount());
		tiosa.setStatus(TransferInOutStockApproval.TRANSFERINOUTSTOCKAPPROVAL_STATUS_ADOPT);
		tiosa.setAuthOperId(webInput.getOperId());
		tiosa.setAuthTime(PCPSCommonDaoImpl.getDatabaseSysTime());
		tiosa.setRemark(vo.getRemark());
		transferInOutStockApprovalDao.updateTransferInOutStockApproval(tiosa);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "调拨单申请审批通过", "ApprovalBranchNo:"
						+ vo.getApprovalBranchNo());
	}

	@Override
	public void AuthTransferInOutStockApprovalRefuse(PCPSWebInput webInput,
			TransferInOutStockApproval vo) throws PCPSApplicationException {
		if (vo == null || vo.getTransferApprovalId() == null
				|| webInput == null || webInput.getOperBranchNo() == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "", "参数为空!");
		TransferInOutStockApproval tiosa = transferInOutStockApprovalDao
				.getTransferInOutStockApprovalByTransferApprovalId(vo
						.getTransferApprovalId());
		if (tiosa == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "",
					"此对象不存在,无法审核拒绝!");
		if (!tiosa.getBranchNo().equals(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_NULLPOINTER, "",
					"不能操作其它机构调拨申请单!");
		}
		if (tiosa.getApprovalOperId() == webInput.getOperId()) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRMSG_UNKNOWN, "",
					"申请人与审核人不能为同一人!");
		}
		tiosa.setAuthOperId(webInput.getOperId());
		tiosa
				.setStatus(TransferInOutStockApproval.TRANSFERINOUTSTOCKAPPROVAL_STATUS_REFUSE);
		tiosa.setAuthTime(PCPSCommonDaoImpl.getDatabaseSysTime());
		tiosa.setRemark(vo.getRemark());
		transferInOutStockApprovalDao.updateTransferInOutStockApproval(tiosa);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "调拨单申请审批拒绝", "ApprovalBranchNo:"
						+ vo.getApprovalBranchNo());
	}

}
